package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.QGoodsFile;
import com.neusoft.neuiotms.dcam.fixed.model.QStock;
import com.neusoft.neuiotms.dcam.fixed.model.Stock;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedStockRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedStockRepositoryImpl extends AbstractRepository implements CustomizedStockRepository {

    @Override
    public Page<Stock> search(final Pageable pageable, final Searchable searchable) {
        final QStock qStock = QStock.stock;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Stock> query = this.query().selectFrom(qStock);
        query.where(where).orderBy(qStock.updateTime.desc(), qStock.createTime.desc());
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return Stock.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QStock qStock = QStock.stock;
        final QGoodsFile qGoodsFile = QGoodsFile.goodsFile;

        if (!StringUtils.isEmpty(searchable.getStrValue("goodsFileId"))) {
            where.and(super.equalsLong(qStock.goodsFile.id, searchable, "goodsFileId"));//货品id
        }
        if (StringUtils.isNotEmpty(searchable.getStrValue("spareTypeId"))) {
            where.and(super.equalsLong(qStock.goodsFile.spareType.id, searchable, "spareTypeId"));//备品类型
        }
        if (StringUtils.isNotEmpty(searchable.getStrValue("spareTypeCode"))) {
            where.and(super.equalsStr(qStock.goodsFile.spareType.code, searchable, "spareTypeCode"));//备品类型
        }
        if (StringUtils.isNotEmpty(searchable.getStrValue("parentCode"))) {
            where.and(super.equalsStr(qGoodsFile.spareType.parent.code, searchable, "parentCode"));//备品类型
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("manufacturerId"))) {
            where.and(super.equalsLong(qStock.manufacturer.id, searchable, "manufacturerId"));//供应商id
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("storehouseId"))) {
            where.and(super.equalsLong(qStock.storehouse.id, searchable, "storehouseId"));//仓库id
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("batch"))) {
            where.and(super.containsStr(qStock.batch, searchable, "batch"));//类型code
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("stockStartTime"))) {
            where.and(super.afterOrEqualsDate(qStock.createTime, searchable, "stockStartTime"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("stockEndTime"))) {
            where.and(super.beforeOrEqualsDate(qStock.createTime, searchable, "stockEndTime"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("globalSearch"))) {
            where.or(super.containsStr(qStock.goodsFile.name, searchable, "globalSearch"));
            where.or(super.containsStr(qStock.goodsFile.specification, searchable, "globalSearch"));
            where.or(super.containsStr(qStock.goodsFile.spareType.name, searchable, "globalSearch"));
            where.or(super.containsStr(qStock.manufacturer.name, searchable, "globalSearch"));
            where.or(super.containsStr(qStock.storehouse.name, searchable, "globalSearch"));
            where.or(super.containsStr(qStock.batch, searchable, "globalSearch"));
        }

        return where;
    }

}
